#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <Eigen/Core>
#include <Eigen/Geometry>

#include <chrono>

#include <vector>

#include <ceres/ceres.h>

#include "PnPSolver.h"

using namespace std;
using namespace cv;

using Point3d = Eigen::Vector3d;
using Vector3d = Eigen::Vector3d;
using Matrix3d = Eigen::Matrix3d;
using MatrixXd = Eigen::MatrixXd;

#define PNP_DBL_EPSILON 2.2204460492503131e-16f
#define PNP_EPSILON 1.19209290e-7f

#define PNP_MIN_VALUE 2.225073858507201383090e-308
#define PNP_MAX_VALUE 1.79769e+308

#define PNP_MAX(a, b) ((a) < (b) ? (b) : (a))
#define PNP_MIN(a, b) ((a) > (b) ? (b) : (a))

// optimizer param
#define TERMCRIT_ITER 1
#define TERMCRIT_NUMBER CV_TERMCRIT_ITER
#define TERMCRIT_EPS 2

static Mat K_;
static double fx_, fy_, cx_, cy_;

template<typename T>
Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> cvMatToEigen(const cv::Mat& cv_mat) {
    Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> eigen_mat;
    eigen_mat.resize(cv_mat.rows, cv_mat.cols);
    
    for (int i = 0; i < cv_mat.rows; ++i) {
        for (int j = 0; j < cv_mat.cols; ++j) {
            eigen_mat(i, j) = cv_mat.at<T>(i, j);
        }
    }
    return eigen_mat;
}

void find_feature_matches (
    const Mat& img_1, const Mat& img_2,
    std::vector<KeyPoint>& keypoints_1,
    std::vector<KeyPoint>& keypoints_2,
    std::vector< DMatch >& matches );

// 像素坐标转相机归一化坐标
Point2d pixel2cam ( const Point2d& p, const Mat& K );

void bundleAdjustment (
    const vector<Point3f> points_3d,
    const vector<Point2f> points_2d,
    const Mat& K,
    Mat& R, Mat& t
);

int main ( int argc, char** argv )
{
    //-- 读取图像
    Mat img_1 = imread ( "../1.png", 1 );
    Mat img_2 = imread ( "../2.png", 1 );

    vector<KeyPoint> keypoints_1, keypoints_2;
    vector<DMatch> matches;
    find_feature_matches ( img_1, img_2, keypoints_1, keypoints_2, matches );
    cout<<"一共找到了"<<matches.size() <<"组匹配点"<<endl;

    // 建立3D点
    Mat d1 = imread ( "../1_depth.png", 1 );       // 深度图为16位无符号数，单通道图像
    Mat K = ( Mat_<double> ( 3,3 ) << 520.9, 0, 325.1, 0, 521.0, 249.7, 0, 0, 1 );
    K_ = K;

    fx_ = 520.9;
    fy_ = 521.0;
    cx_ = 325.1;
    cy_ = 249.7;

    vector<Point3f> pts_3d;
    vector<Point2f> pts_2d;
    for ( DMatch m:matches )
    {
        ushort d = d1.ptr<unsigned short> (int ( keypoints_1[m.queryIdx].pt.y )) [ int ( keypoints_1[m.queryIdx].pt.x ) ];
        if ( d == 0 )   // bad depth
            continue;
        float dd = d/5000.0;
        Point2d p1 = pixel2cam ( keypoints_1[m.queryIdx].pt, K );
        pts_3d.push_back ( Point3f ( p1.x*dd, p1.y*dd, dd ) );
        pts_2d.push_back ( keypoints_2[m.trainIdx].pt );
    }

    cout<<"3d-2d pairs: "<<pts_3d.size() <<endl;

    Mat r, t;
    solvePnP ( pts_3d, pts_2d, K, Mat(), r, t, false, SOLVEPNP_EPNP); // 调用OpenCV 的 PnP 求解，可选择EPNP，DLS等方法
    // solvePnP ( pts_3d, pts_2d, K, Mat(), r, t, false ); // 调用OpenCV 的 PnP 求解，可选择EPNP，DLS等方法

    Mat R;
    cv::Rodrigues ( r, R ); // r为旋转向量形式，用Rodrigues公式转换为矩阵

    cout<<"R="<<endl<<R<<endl;
    cout<<"t="<<endl<<t<<endl;
 
    cout<<"calling bundle adjustment"<<endl;

    bundleAdjustment ( pts_3d, pts_2d, K, R, t );
}

void find_feature_matches ( const Mat& img_1, const Mat& img_2,
                            std::vector<KeyPoint>& keypoints_1,
                            std::vector<KeyPoint>& keypoints_2,
                            std::vector< DMatch >& matches )
{
    //-- 初始化
    Mat descriptors_1, descriptors_2;
    // used in OpenCV3
    Ptr<FeatureDetector> detector = ORB::create();
    Ptr<DescriptorExtractor> descriptor = ORB::create();
    // use this if you are in OpenCV2
    // Ptr<FeatureDetector> detector = FeatureDetector::create ( "ORB" );
    // Ptr<DescriptorExtractor> descriptor = DescriptorExtractor::create ( "ORB" );
    Ptr<DescriptorMatcher> matcher  = DescriptorMatcher::create ( "BruteForce-Hamming" );
    //-- 第一步:检测 Oriented FAST 角点位置
    detector->detect ( img_1,keypoints_1 );
    detector->detect ( img_2,keypoints_2 );

    //-- 第二步:根据角点位置计算 BRIEF 描述子
    descriptor->compute ( img_1, keypoints_1, descriptors_1 );
    descriptor->compute ( img_2, keypoints_2, descriptors_2 );

    //-- 第三步:对两幅图像中的BRIEF描述子进行匹配，使用 Hamming 距离
    vector<DMatch> match;
    // BFMatcher matcher ( NORM_HAMMING );
    matcher->match ( descriptors_1, descriptors_2, match );

    //-- 第四步:匹配点对筛选
    double min_dist=10000, max_dist=0;

    //找出所有匹配之间的最小距离和最大距离, 即是最相似的和最不相似的两组点之间的距离
    for ( int i = 0; i < descriptors_1.rows; i++ )
    {
        double dist = match[i].distance;
        if ( dist < min_dist ) min_dist = dist;
        if ( dist > max_dist ) max_dist = dist;
    }

    printf ( "-- Max dist : %f \n", max_dist );
    printf ( "-- Min dist : %f \n", min_dist );

    //当描述子之间的距离大于两倍的最小距离时,即认为匹配有误.但有时候最小距离会非常小,设置一个经验值30作为下限.
    for ( int i = 0; i < descriptors_1.rows; i++ )
    {
        if ( match[i].distance <= max ( 2*min_dist, 30.0 ) )
        {
            matches.push_back ( match[i] );
        }
    }

    Mat img_matches;
    drawMatches( img_1, keypoints_1, img_2, keypoints_2, matches, img_matches );
    imshow("ORB Matches", img_matches);
    waitKey(0);
}

Point2d pixel2cam ( const Point2d& p, const Mat& K )
{
    return Point2d
           (
               ( p.x - K.at<double> ( 0,2 ) ) / K.at<double> ( 0,0 ),
               ( p.y - K.at<double> ( 1,2 ) ) / K.at<double> ( 1,1 )
           );
}


void MyRodrigues(const Vector3d &r_vec, Matrix3d &R, MatrixXd &jacobian)
{
    double J[27] = {0};
    Vector3d r = r_vec;
    jacobian = MatrixXd(3, 9);

    double theta = r.norm();

    if (theta < PNP_EPSILON)
    {
        R.setIdentity();

        memset(J, 0, sizeof(J));
        J[5] = J[15] = J[19] = -1;
        J[7] = J[11] = J[21] = 1;
    }
    else
    {
        double c = std::cos(theta);
        double s = std::sin(theta);
        double c1 = 1. - c;
        double itheta = theta ? 1. / theta : 0.;

        r *= itheta;

        Matrix3d rrt;
        rrt << r[0] * r[0], r[0] * r[1], r[0] * r[2], 
               r[0] * r[1], r[1] * r[1], r[1] * r[2], 
               r[0] * r[2], r[1] * r[2], r[2] * r[2];
        
        Matrix3d r_x;
        r_x << 0, -r[2], r[1],
               r[2], 0, -r[0],
               -r[1], r[0], 0;

        // R = cos(theta)*I + (1 - cos(theta))*r*rT + sin(theta)*[r_x]
        R = c * Matrix3d::Identity() + c1 * rrt + s * r_x;

        // compute jacobian
        const double I[] = {1, 0, 0, 0, 1, 0, 0, 0, 1};
        double drrt[] = {r[0] + r[0], r[1], r[2], r[1], 0, 0, r[2], 0, 0,
                        0, r[0], 0, r[0], r[1] + r[1], r[2], 0, r[2], 0,
                        0, 0, r[0], 0, 0, r[1], r[0], r[1], r[2] + r[2]};

        double d_r_x_[] = {0, 0, 0, 0, 0, -1, 0, 1, 0,
                          0, 0, 1, 0, 0, 0, -1, 0, 0,
                          0, -1, 0, 1, 0, 0, 0, 0, 0};

        for (uint32_t i = 0; i < 3; i++)
        {
            double ri = i == 0 ? r[0] : i == 1 ? r[1] : r[2];
            double a0 = -s * ri, a1 = (s - 2 * c1 * itheta) * ri, a2 = c1 * itheta;
            double a3 = (c - s * itheta) * ri, a4 = s * itheta;

            for (uint32_t k = 0; k < 9; k++)
            {
                J[i * 9 + k] = a0 * I[k] + a1 * rrt(k / 3, k % 3) + a2 * drrt[i * 9 + k] +
                               a3 * r_x(k / 3, k % 3) + a4 * d_r_x_[i * 9 + k];
            }
        }
    }

    for (uint32_t r = 0; r < 3; r++)
    {
        for (uint32_t c = 0; c < 9; c++)
        {
            jacobian(r, c) = J[r * 9 + c];
        }
    }
}

// 重投影函数
void ProjectPoints3D2D(const std::vector<Eigen::Vector3d> &inlier_landmarks,
                   const std::vector<Eigen::Vector3d> &inlier_bearings,
                   const Eigen::Vector3d &rotation_vector,
                   const Eigen::Vector3d &translation_vector,
                   Eigen::MatrixXd &dpdrot, Eigen::MatrixXd &dpdt,
                   Eigen::MatrixXd &reproject_err,
                   const bool &calc_derivative)
{
    double k[14] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

    // double fx = fx_, fy = fy_, cx = cx_, cy = cy_;
    double fx = 1, fy = 1, cx = 0, cy = 0;

    Matrix3d matR;
    Vector3d t = translation_vector;
    MatrixXd dRdr = MatrixXd(3, 9);
    Matrix3d matTilt = Matrix3d::Identity();

    MyRodrigues(rotation_vector, matR, dRdr);

    uint32_t count = inlier_landmarks.size();
    reproject_err = Eigen::MatrixXd(2 * count, 1);

    if (calc_derivative) {
        dpdrot = MatrixXd(2 * count, 3);
        dpdt = MatrixXd(2 * count, 3);
    }

    for (uint32_t i = 0; i < count; i++) {
        const Vector3d &pos_w = inlier_landmarks.at(i);
        Eigen::Vector3d pc = matR * pos_w + t;

        double X = pos_w[0], Y = pos_w[1], Z = pos_w[2];
        double x = pc[0];
        double y = pc[1];
        double z = pc[2];

        // 归一化坐标
        z = z ? 1. / z : 1;
        x *= z;
        y *= z;

        // 畸变模型
        double r2 = x * x + y * y;
        double r4 = r2 * r2;
        double r6 = r4 * r2;
        double a1 = 2 * x * y;
        double a2 = r2 + 2 * x * x;
        double a3 = r2 + 2 * y * y;
        double cdist = 1 + k[0] * r2 + k[1] * r4 + k[4] * r6;
        
        double icdist2 = 1. / (1 + k[5] * r2 + k[6] * r4 + k[7] * r6);
        double xd0 = x * cdist * icdist2 + k[2] * a1 + k[3] * a2 + k[8] * r2 + k[9] * r4;
        double yd0 = y * cdist * icdist2 + k[2] * a3 + k[3] * a1 + k[10] * r2 + k[11] * r4;

        // 倾斜平面投影
        Eigen::Vector3d vecTilt = matTilt * Vector3d(xd0, yd0, 1);
        double invProj = vecTilt(2) ? 1. / vecTilt(2) : 1;
        double xd = invProj * vecTilt(0);
        double yd = invProj * vecTilt(1);

        // 像素坐标
        double ptx = xd * fx + cx;
        double pty = yd * fy + cy;

        // cout << "vecTilt : " << vecTilt.transpose() << endl;
        // cout << "xd ==== : " << xd << ", " << yd << endl;

        const Eigen::Vector3d &norm_point = inlier_bearings.at(i);
        
        // 重投影误差
        reproject_err(2 * i, 0) = ptx - (norm_point[0] / norm_point[2] * fx + cx);
        reproject_err(2 * i + 1, 0) = pty - (norm_point[1] / norm_point[2] * fy + cy);

        // cout << "norm_point ==== : " << norm_point.transpose() << endl;
        // cout << "reprojection error: " << reproject_err(2 * i, 0) << ", " << reproject_err(2 * i + 1, 0) << endl;

        if (calc_derivative) {
            Eigen::Matrix2d dMatTilt;
            for (int row = 0; row < 2; ++row) {
                for (int col = 0; col < 2; ++col) {
                    dMatTilt(row, col) = matTilt(row, col) * vecTilt(2) - matTilt(2, col) * vecTilt(row);
                }
            }
            double invProjSquare = (invProj * invProj);
            dMatTilt *= invProjSquare;

            // dpdt 对平移的导数
            double dxdt[] = {z, 0, -x * z}, dydt[] = {0, z, -y * z};
            for (int j = 0; j < 3; j++) {
                double dr2dt = 2 * x * dxdt[j] + 2 * y * dydt[j];
                double dcdist_dt = k[0] * dr2dt + 2 * k[1] * r2 * dr2dt + 3 * k[4] * r4 * dr2dt;
                double dicdist2_dt = -icdist2 * icdist2 * (k[5] * dr2dt + 2 * k[6] * r2 * dr2dt + 3 * k[7] * r4 * dr2dt);
                double da1dt = 2 * (x * dydt[j] + y * dxdt[j]);
                double dmxdt = (dxdt[j] * cdist * icdist2 + x * dcdist_dt * icdist2 + x * cdist * dicdist2_dt +
                                k[2] * da1dt + k[3] * (dr2dt + 4 * x * dxdt[j]) + k[8] * dr2dt + 2 * r2 * k[9] * dr2dt);
                double dmydt = (dydt[j] * cdist * icdist2 + y * dcdist_dt * icdist2 + y * cdist * dicdist2_dt +
                                k[2] * (dr2dt + 4 * y * dydt[j]) + k[3] * da1dt + k[10] * dr2dt + 2 * r2 * k[11] * dr2dt);
                Eigen::Vector2d dXdYd = dMatTilt * Eigen::Vector2d(dmxdt, dmydt);

                dpdt(2 * i, j) = fx * dXdYd(0);
                dpdt(2 * i + 1, j) = fy * dXdYd(1);
            }

            // dpdr 对旋转的导数
            double dx0dr[] = {
                X * dRdr(0, 0) + Y * dRdr(0, 1) + Z * dRdr(0, 2),
                X * dRdr(1, 0) + Y * dRdr(1, 1) + Z * dRdr(1, 2),
                X * dRdr(2, 0) + Y * dRdr(2, 1) + Z * dRdr(2, 2)};
            double dy0dr[] = {
                X * dRdr(0, 3) + Y * dRdr(0, 4) + Z * dRdr(0, 5),
                X * dRdr(1, 3) + Y * dRdr(1, 4) + Z * dRdr(1, 5),
                X * dRdr(2, 3) + Y * dRdr(2, 4) + Z * dRdr(2, 5)};
            double dz0dr[] = {
                X * dRdr(0, 6) + Y * dRdr(0, 7) + Z * dRdr(0, 8),
                X * dRdr(1, 6) + Y * dRdr(1, 7) + Z * dRdr(1, 8),
                X * dRdr(2, 6) + Y * dRdr(2, 7) + Z * dRdr(2, 8)};
                    
            for (int j = 0; j < 3; j++) {
                double dxdr = z * (dx0dr[j] - x * dz0dr[j]);
                double dydr = z * (dy0dr[j] - y * dz0dr[j]);
                double dr2dr = 2 * x * dxdr + 2 * y * dydr;
                double dcdist_dr = (k[0] + 2 * k[1] * r2 + 3 * k[4] * r4) * dr2dr;
                double dicdist2_dr = -icdist2 * icdist2 * (k[5] + 2 * k[6] * r2 + 3 * k[7] * r4) * dr2dr;
                double da1dr = 2 * (x * dydr + y * dxdr);
                double dmxdr = (dxdr * cdist * icdist2 + x * dcdist_dr * icdist2 + x * cdist * dicdist2_dr +
                                k[2] * da1dr + k[3] * (dr2dr + 4 * x * dxdr) + (k[8] + 2 * r2 * k[9]) * dr2dr);
                double dmydr = (dydr * cdist * icdist2 + y * dcdist_dr * icdist2 + y * cdist * dicdist2_dr +
                                k[2] * (dr2dr + 4 * y * dydr) + k[3] * da1dr + (k[10] + 2 * r2 * k[11]) * dr2dr);

                Eigen::Vector2d dXdYd = dMatTilt * Eigen::Vector2d(dmxdr, dmydr);
         
                dpdrot(2 * i, j) = fx * dXdYd(0);
                dpdrot(2 * i + 1, j) = fy * dXdYd(1);
            }
        }
    }
}

// 使用解析导数的Ceres代价函数
class AnalyticReprojectionError : public ceres::SizedCostFunction<2, 3, 3> {
public:
    AnalyticReprojectionError(const Eigen::Vector3d& landmark, const Eigen::Vector3d& bearing)
        : landmark_(landmark), bearing_(bearing) {}

    virtual bool Evaluate(double const* const* parameters,
                         double* residuals,
                         double** jacobians) const {
        const double* rotation = parameters[0];
        const double* translation = parameters[1];
        
        Vector3d rot_vec(rotation[0], rotation[1], rotation[2]);
        Vector3d trans_vec(translation[0], translation[1], translation[2]);
        
        vector<Eigen::Vector3d> landmarks = {landmark_};
        vector<Eigen::Vector3d> bearings = {bearing_};
        
        MatrixXd dpdrot, dpdt, reproj_err;
        ProjectPoints3D2D(landmarks, bearings, rot_vec, trans_vec, dpdrot, dpdt, reproj_err, jacobians != nullptr);
        
        // cout << "reprojection error: " << reproj_err << endl;

        residuals[0] = reproj_err(0, 0);
        residuals[1] = reproj_err(1, 0);
        
        if (jacobians != nullptr) {
            if (jacobians[0] != nullptr) {
                // 旋转的雅可比
                for (int i = 0; i < 2; ++i) {
                    for (int j = 0; j < 3; ++j) {
                        jacobians[0][i * 3 + j] = dpdrot(i, j);
                    }
                }
            }
            
            if (jacobians[1] != nullptr) {
                // 平移的雅可比
                for (int i = 0; i < 2; ++i) {
                    for (int j = 0; j < 3; ++j) {
                        jacobians[1][i * 3 + j] = dpdt(i, j);
                    }
                }
            }
        }
        
        return true;
    }

private:
    const Eigen::Vector3d landmark_;
    const Eigen::Vector3d bearing_;
};


void bundleAdjustment (
    const vector< Point3f > points_3d,
    const vector< Point2f > points_2d,
    const Mat& K,
    Mat& R, Mat& t )
{

    cout << "=== PnP Solver with Ceres Optimization ===" << endl;
    
    // 生成测试数据
    vector<Eigen::Vector3d> landmarks;
    vector<Eigen::Vector3d> bearings;
    
    // 添加一些测试点
    for (int i = 0; i < points_3d.size(); ++i) {
        landmarks.push_back(Eigen::Vector3d(points_3d[i].x, points_3d[i].y, points_3d[i].z));
        Point2d norm_pt = pixel2cam ( Point2d(points_2d[i].x, points_2d[i].y), K );
        bearings.push_back(Eigen::Vector3d(norm_pt.x, norm_pt.y, 1.0));
    }
    Matrix3d eigenR = cvMatToEigen<double>(R);
    Vector3d eigent = cvMatToEigen<double>(t);
    Mat rotation_vector;
    cv::Rodrigues(R, rotation_vector);
    Vector3d eigen_rotation_vector = cvMatToEigen<double>(rotation_vector);

    // 初始位姿估计
    Vector3d initial_rotation(eigen_rotation_vector);
    Vector3d initial_translation(eigent);
    
    // 测试rodrigues函数

    MatrixXd jacobian;
    MyRodrigues(initial_rotation, eigenR, jacobian);
    cout << "Initial rotation vector: " << initial_rotation.transpose() << endl;
    cout << "Rotation matrix:\n" << R << endl;
    
    // 构建优化问题
    ceres::Problem problem;
    
    for (size_t i = 0; i < landmarks.size(); ++i) {
        // 使用解析导数（推荐，因为我们已经计算了精确的雅可比矩阵）
        ceres::CostFunction* cost_function = 
            new AnalyticReprojectionError(landmarks[i], bearings[i]);
        
        // 或者使用自动微分
        // ceres::CostFunction* cost_function = 
        //     AutoDiffReprojectionError::Create(landmarks[i], bearings[i]);
        
        problem.AddResidualBlock(cost_function, 
                               nullptr,  // 损失函数
                               initial_rotation.data(), 
                               initial_translation.data());
    }
    
    // 配置优化选项
    ceres::Solver::Options options;
    options.linear_solver_type = ceres::DENSE_QR;
    options.minimizer_progress_to_stdout = true;
    options.max_num_iterations = 100;
    options.function_tolerance = 1e-10;
    options.gradient_tolerance = 1e-10;
    options.parameter_tolerance = 1e-10;
    
    // 运行优化
    ceres::Solver::Summary summary;
    ceres::Solve(options, &problem, &summary);
    
    // 输出结果
    cout << "\n=== Optimization Results ===" << endl;
    cout << summary.BriefReport() << endl;
    cout << "Initial rotation: " << eigen_rotation_vector.transpose() << endl;
    cout << "Initial translation: " << eigent.transpose() << endl;
    cout << "Final rotation: " << initial_rotation.transpose() << endl;
    cout << "Final translation: " << initial_translation.transpose() << endl;
    
    // 验证最终的重投影误差
    MatrixXd dpdrot, dpdt, final_reproj_err;
    ProjectPoints3D2D(landmarks, bearings, initial_rotation, initial_translation, 
                  dpdrot, dpdt, final_reproj_err, false);
    
    double total_error = final_reproj_err.norm();
    cout << "Final reprojection error: " << total_error << endl;

}
